package arrayListExercises.studentManagementSystem;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author: bamboo_shoot
 * @date: 20/2/5 22:18
 * @description: 用arrayList来写一个学生管理系统
 */
public class StudentManagement {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>();
        while (true) {
            System.out.println("----------欢迎来到学生管理系统------------");
            System.out.println("1 查看所有学生");
            System.out.println("2 添加学生");
            System.out.println("3 删除学生");
            System.out.println("4 修改学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择：");
//            这里是键盘录入然后根据不同的选择
            Scanner sc = new Scanner(System.in);
            int studentNumber = sc.nextInt();
            switch (studentNumber) {
                case 1:
                    toSeeAll(students);
                    break;
                case 2:
                    AddStudent(students);
                    break;
                case 3:
                    removeStudent(students);
                    break;
                case 4:
                    modifyStudentInformation(students);
                    break;
                case 5:
                    System.out.println("谢谢使用");
                    System.exit(0);
                default:
                    System.out.println("输入有误请重新输入");

            }
        }
    }

    private static void modifyStudentInformation(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学号");
        String studentId = sc.nextLine();

//        先设置一个索引假设为-1
        int index = -1;
//        如果遍历集合，输入的id==遍历后找到的id，那么让假设的这个索引=i
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(studentId)) {
                index = i;
            }
        }
        if (index == -1) {
            System.out.println("输入的学号错误");
        } else {
            System.out.println("请输入新的姓名");
            String name = sc.nextLine();
            System.out.println("请输入新的年龄");
            String age = sc.nextLine();
            System.out.println("请输入新的地址");
            String address = sc.nextLine();
            Student student = new Student(studentId,name,age,address);
            students.set(index, student);
        }
    }

    private static void removeStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        String studentNumber = sc.nextLine();
        if (students.size() > 0) {
            for (int i = 0; i < students.size(); i++) {
                if (students.get(i).getId().equals(studentNumber)) {
                    students.remove(i);
                    System.out.println("删除学生成功");
                } else {
                    System.out.println("删除的学号不存在");
                }
            }
        } else {
            System.out.println("删除的学号不存在");
        }
    }

    private static void AddStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String id = sc.nextLine();
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        System.out.println("请输入地址");
        String address = sc.nextLine();
        Student student = new Student(id, name, age, address);
        students.add(student);
        System.out.println("添加成功");
    }

    private static void toSeeAll(ArrayList<Student> students) {
        if (students.size() == 0) {
            System.out.println("目前没有学生信息，请添加！");
        } else {
            System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "地址");
            for (int i = 0; i < students.size(); i++) {
                System.out.println(students.get(i).getId() + "\t" + students.get(i).getName() + "\t" + students.get(i).getAge() + "\t" + students.get(i).getAddress());
            }
        }
    }


}
